package com.ygs.secs.controller;


import com.ygs.secs.model.Admin;
import com.ygs.secs.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员模块控制器
 *
 * @author Zym
 */
@Component
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;


    /**
     * 管理员登录
     *
     * @param adminid  账号
     * @param password 密码
     * @return 通用json返回格式
     */
    @RequestMapping("/adminlogin")
    @ResponseBody
    public Map<String, Object> adminlogin(String adminid, String password) {
        Map<String, Object> map = new HashMap<>();
        try {
            Admin admin = adminService.selectByPrimaryKey(adminid);
            if (admin != null) {
                if (admin.getPassword().equals(password)) {
                    map.put("code", 200);
                    map.put("msg", "登录成功");
                    map.put("data", admin);
                    map.put("admintype", 520);
                } else {
                    map.put("code", 201);
                    map.put("msg", "密码错误");
                    map.put("data", null);
                }
            } else {
                map.put("code", 500);
                map.put("msg", "账户不存在，请联系相关工作人员");
                map.put("data", null);
            }

        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 查询admin账户信息
     *
     * @param adminid 企业ID
     * @return 通用json返回格式
     */
    @RequestMapping("/getadminByid")
    @ResponseBody
    public Map<String, Object> getAdminData(String adminid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Admin admin = adminService.selectByPrimaryKey(adminid);
            if (admin != null) {
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", admin);
            } else {
                map.put("code", 201);
                map.put("msg", "数据查询失败，没有此账户");
                map.put("data", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "system error ");
        }
        return map;
    }

    /**
     * 修改admin账户信息
     *
     * @param adminid adminID
     * @return 通用json返回格式
     */
    @RequestMapping("/updateadmin")
    @ResponseBody
    public Map<String, Object> updateAdmin(String adminid, String realname, String contactnumber) {
        Map<String, Object> map = new HashMap<>();
        Admin admin = adminService.selectByPrimaryKey(adminid);
        if (admin.getAdminid().equals(adminid)) {
            admin.setRealname(realname);
            admin.setContactnumber(contactnumber);
            try {
                int i = adminService.updateByPrimaryKey(admin);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "资料修改成功");
                    map.put("data", admin);
                }
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code", 500);
                map.put("msg", "系统错误");
            }
        } else {
            map.put("code", 201);
            map.put("msg", "修改失败，请重试！");
        }
        return map;
    }

    /**
     * 修改admin 密码
     *
     * @param adminid adminID
     * @return 通用json返回格式
     */
    @RequestMapping("/updateadminPwd")
    @ResponseBody
    public Map<String, Object> updateAdminPwd(String adminid, String oldPwd, String newPwd) {
        Map<String, Object> map = new HashMap<>();
        Admin admin = adminService.selectByPrimaryKey(adminid);
        if (admin.getPassword().equals(oldPwd)) {
            admin.setPassword(newPwd);
            try {
                int i = adminService.updateByPrimaryKey(admin);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "密码修改成功");
                    map.put("data", admin);
                }
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code", 500);
                map.put("msg", "系统错误");
            }
        } else {
            map.put("code", 201);
            map.put("msg", "修改失败，请重试！");
        }
        return map;
    }
}
